package net.oschina.aibinxiao.job;

import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: AiBinXiao
 * @Description: 获取jobDataMap的方式一：直接从Map中获取
 * @Date: 2017/10/12 8:35
 */

public class HelloJob1 implements Job{
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 打印当前时间，格式为 yyyy-MM-dd HH:mm:ss
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateStr = sdf.format(date);
        System.out.println("当前时间是：" + dateStr);
        // 编写具体业务逻辑类
        JobKey jobKey = context.getJobDetail().getKey();
        System.out.println("myjob1 name is: " + jobKey.getName() + " and group is: " + jobKey.getGroup());
        TriggerKey triggerKey = context.getTrigger().getKey();
        System.out.println("mytrigger name is: " + triggerKey.getName() + " and group is " + triggerKey.getGroup());

        // 获取自定义传入参数
        /*JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        JobDataMap triggerDataMap = context.getTrigger().getJobDataMap();*/

        // 前面两个可以合并为一个MergedJobDataMap
        // 注意，当job和trigger中有key值相同的情况下，会优先显示trigger中的值，即trigger会覆盖job中的值
        JobDataMap dataMap = context.getMergedJobDataMap();

        String jobMessage = dataMap.getString("message");
        Float mathJobValue = dataMap.getFloat("mathJobValue");
        String triggerMessage = dataMap.getString("message");
        Double doubleTriggerValue = dataMap.getDouble("doubleTriggerValue");
        System.out.println("jobMessage is :" + jobMessage);
        System.out.println("mathJobValue id :" + mathJobValue);
        System.out.println("triggerMessage is:" + triggerMessage);
        System.out.println("doubleTriggerValue is:" + doubleTriggerValue);
    }
}
